package com.cy.helmet;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;


import com.nan.gbd.library.GB28181ControlManager;
import com.nan.gbd.library.codec.BaseGBMedia;
import com.nan.gbd.library.codec.GBMediaNative;
import com.nan.gbd.library.codec.configuration.ConfigCallBack;
import com.nan.gbd.library.codec.configuration.Configuration;
import com.nan.gbd.library.utils.AccountConfig;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class MainActivity2 extends AppCompatActivity {
    private static final int REQUEST_PERMISSION_ID = 20;
    public static final int REQUEST_SETTINGS = 100;
    private boolean isPermissionGranted = false;
    private static final String[] REQUESTED_PERMISSIONS = {
            Manifest.permission.CAMERA,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
    };

    ImageView iv_record;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv_record = findViewById(R.id.iv_record);
        iv_record.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                recode(v);
            }
        });
        checkSelfPermission(REQUEST_PERMISSION_ID);
    }

    private void checkSelfPermission(int requestCode) {
        for (String permission : REQUESTED_PERMISSIONS) {
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                isPermissionGranted = false;
            }
        }
        if (!isPermissionGranted) {
            ActivityCompat.requestPermissions(this, REQUESTED_PERMISSIONS, requestCode);
        } else {

        }
        init();
    }

    private void init() {
        File file = getAudioDir();
        if (file.exists()) {
            file.mkdirs();
        }
        Configuration configuration = Configuration.get();
        configuration.setmContext(getApplicationContext());
        configuration.setLiveCodeRate(1300);
        configuration.setLiveResolution(new int[]{1280, 720});
        configuration.setMaxRecordFileLength(50);
        configuration.setRecordCodeRate(1300);
        configuration.setRecordPath(file.getAbsolutePath());
        configuration.setRecordResolution(new int[]{1280, 720});
        configuration.setConfigCallBack(new ConfigCallBack() {
            @Override
            public boolean isWritable() {
                return true;
            }

            @Override
            public Map<String, File> getGB28181EmptyVideoFile() {
                return getEmptyVideoFile();
            }
        });
        AccountConfig.getInstance().initConfig();
        GBMediaNative.getInstance().setHelmetVoiceCallBack(new BaseGBMedia.HelmetVoiceCallBack() {
            @Override
            public void playRemindVoice() {

            }

            @Override
            public void stopRemindVoice() {

            }

            @Override
            public void playLiveVoice() {

            }

            @Override
            public void stopLiveVoice() {

            }
        });
        GB28181ControlManager.getInstance().init();

    }

    private Map<String, File> getEmptyVideoFile() {
        SimpleDateFormat formater = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String pureFileName = formater.format(new Date());
        String fileName = pureFileName + ".mp4";
        String mediaSubDirName = pureFileName.substring(0, 8);
        Map<String, File> fileMap = new HashMap<>();
        File rootDir = getAudioDir();
        File mediaSubDir = new File(rootDir, mediaSubDirName);
        if (!mediaSubDir.exists()) {
            mediaSubDir.mkdirs();
        } else if (!mediaSubDir.isDirectory()) {
            mediaSubDir.delete();
            mediaSubDir.mkdirs();
        }
        fileMap.put("file_super", mediaSubDir);
        if (mediaSubDir != null) {
            File childFile = new File(mediaSubDir, fileName);
            fileMap.put("file_child", childFile);
            return fileMap;
        }
        return null;
    }

    public static File getAudioDir() {
        String rootdir = "zhq";
        String recorddir = "gb28181";
        File sdFile = Environment.getExternalStorageDirectory();
        File rootDir = new File(sdFile, rootdir);
        if (!rootDir.exists()) {
            rootDir.mkdirs();
        }

        File recordDir = new File(rootDir, recorddir);
        if (!recordDir.exists()) {
            recordDir.mkdirs();
        }

        return recordDir;
    }

    public void recode(View view) {
        //录像
        if (!GBMediaNative.getInstance().isRunning() && !GBMediaNative.getInstance().isRecording()) {
            GBMediaNative.getInstance().startRecord();
            iv_record.setImageResource(R.mipmap.ic_stop_record);
        } else {
            GBMediaNative.getInstance().stopRecord();
            iv_record.setImageResource(R.mipmap.ic_start_record);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_PERMISSION_ID: {
                for (int i = 0; i < grantResults.length; i++) {
                    if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                        isPermissionGranted = false;
                        finish();
                        return;
                    }
                }
                isPermissionGranted = true;
                init();
                break;
            }
        }
    }
}
